package com.longshihan.carservice.controller;

import com.google.gson.reflect.TypeToken;
import com.longshihan.carservice.http.APIResponse;
import com.longshihan.carservice.model.OrderGoodsDomain;
import com.longshihan.carservice.service.Cart.CartService;
import com.longshihan.carservice.utils.GsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author longhe
 * @time
 */
@Api("购物车相关")
@Controller
@RequestMapping("/cart")
public class CartController extends BaseController {


    @Autowired
    private CartService cartService;


    @ApiOperation(value = "更新购物车")
    @PostMapping(value = "/updateCarts")
    @ResponseBody
    public APIResponse updateCarts(HttpServletRequest request, HttpServletResponse response,
                               @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
                               @ApiParam(name = "goods", value = "数据", required = true) @RequestParam(name = "goods", required = true) String goods) {
        try {
            Type type = new TypeToken<List<OrderGoodsDomain>>() {
            }.getType();
            List<OrderGoodsDomain> cartBeans = (List<OrderGoodsDomain>) GsonUtils.JSONToObject(goods, type);
            cartService.updateGoods(userId, cartBeans);
            return APIResponse.success();
        } catch (Exception e) {
            String msg = "更新购物车失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "更新购物车")
    @PostMapping(value = "/updateCart")
    @ResponseBody
    public APIResponse updateCart(HttpServletRequest request, HttpServletResponse response,
                                   @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
                                   @ApiParam(name = "goods", value = "数据", required = true) @RequestParam(name = "goods", required = true) String goods) {
        try {
            OrderGoodsDomain cartBean = (OrderGoodsDomain) GsonUtils.JSONToObject(goods, OrderGoodsDomain.class);
            cartService.updateGood(userId, cartBean);
            return APIResponse.success();
        } catch (Exception e) {
            String msg = "更新购物车失败";
            return APIResponse.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "获取所有的商品")
    @PostMapping(value = "/getCartGoods")
    @ResponseBody
    public APIResponse getCartGoods(HttpServletRequest request, HttpServletResponse response,
                                    @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
                                    @ApiParam(name = "pageNum", value = "页面数", defaultValue = "1") @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                    @ApiParam(name = "pagesize", value = "单次查询的个数", required = true, defaultValue = "20") @RequestParam(name = "pagesize", required = true, defaultValue = "20") int pagesize
    ) {
        try {
            if (pageNum == 0) {
                pageNum = 1;
            }
            if (pagesize == 0) {
                pagesize = 20;
            }
//            PageInfo<CartBean> logDomainPageInfo=cartService.getCartGoods(userId);
            List<OrderGoodsDomain> cartGoods = cartService.getCartGoods(userId);
            if (cartGoods != null) {
                return APIResponse.success(GsonUtils.toJsonString(cartGoods));
            } else {
                return APIResponse.fail("获取不到购物车信息");
            }

        } catch (Exception e) {
            String msg = "查询购物车信息失败";
            return APIResponse.fail(e.getMessage());
        }
    }
}
